<?php Workerman\Protocols\Http::sessionStart(); $G_MODEL_NAME = basename(__FILE__,'.js.php'); $G_MODEL_URL = $_SESSION["MES_WEB_URL"].'/'. $G_MODEL_NAME; ?>
<?php $access_token = $_SESSION["ACCESS_TOKEN"];?>
$('[name="mod_debuger_input_recv"]').jsonFormatter({ quoteKeys: false, collapsible: false, hideOriginal: true });

$('[name="mod_debuger_input_history"]').click(function(){
    var t = $('[name="mod_debuger_input_history"]').find("option:selected").val();
    var param = {url:"<?php echo $G_MODEL_URL;?>",type:"POST",dataType:"JSON",data:{"action":"select","id":t},access_token:"<?php echo $access_token?>"};
    ajax_request(param,function(result){
        var result =  result["rtval"];
		$('[name="mod_debuger_input_send"]').val(result['msg']);
    })
	
	
});

var param = {url:"<?php echo $G_MODEL_URL;?>",type:"POST",dataType:"JSON",data:$('[name="mod_debuger_input_send"]').val(),access_token:"<?php echo $access_token?>"};
ajax_request(param,function(result){
    console.log(result)
    var obj = result["rtval"];
    for(v in obj["data"]){
        var id = obj["data"][v]["id"];
        var command = obj["data"][v]["command"];
        $('[name="mod_debuger_input_history"]').append("<option value=\""+id+"\">"+ command.substring(0,200) +"</option>");
    }
})

function mod_fun_action(key){
	try{
		if(key=="send"){
			console.log($('[name="mod_debuger_input_send"]').val());
            //setInterval(
            //function(){
            var param = {url:$("#mod_input_<?php echo $G_MODEL_NAME; ?>_server_addr").val(),type:"POST",dataType:"JSON",data:$('[name="mod_debuger_input_send"]').val(),access_token:"<?php echo $access_token?>"};
            ajax_request(param,function(result){
                var command = $('[name="mod_debuger_input_send"]').val();
                var param = {url:"<?php echo $G_MODEL_URL;?>",type:"POST",dataType:"TEXT",data:{action:"add",
                command:command},access_token:"<?php echo $access_token?>"};
                ajax_request(param,function(result2){
                    var result2 =  JSON.parse(result2)["rtval"];
                    if(result2=="done"){
                        //刷新数据
                        $('[name="mod_debuger_input_history"]').empty();
                        var param = {url:"<?php echo $G_MODEL_URL;?>",type:"POST",dataType:"JSON",data:$('[name="mod_debuger_input_send"]').val(),access_token:"<?php echo $access_token?>"};
                        ajax_request(param,function(result3){
                            var obj = result3["rtval"];
                            for(v in obj["data"]){
                                var id = obj["data"][v]["id"];
                                var command = obj["data"][v]["command"];
                                $('[name="mod_debuger_input_history"]').append("<option value=\""+id+"\">"+ command.substring(0,200) +"</option>");
                            }
                        })								

                    }else{
                        alert(result2);
                    }
                    $('[name="mod_debuger_input_recv"]').html(JSON.stringify(result));
                    $('[name="mod_debuger_input_recv"]').jsonFormatter({ quoteKeys: false, collapsible: false, hideOriginal: true });
                    
                })
            })
			return;
		}
	}catch(e){}
	return;
}

//右上角菜单按键
$(".mod_btn_<?php echo $G_MODEL_NAME; ?>").on("click", function(e){
	mod_fun_action($(this).attr("data-tap"));
	return;
});

